package top.fengyulou.xiaofeng.controller.admin;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import top.fengyulou.xiaofeng.common.Jr;
import top.fengyulou.xiaofeng.common.UserUtils;
import top.fengyulou.xiaofeng.dto.PaperQuestionDto;
import top.fengyulou.xiaofeng.dto.QuestionDto;
import top.fengyulou.xiaofeng.service.PaperQuestionService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 后台用户管理
 */
@Controller
public class AdminPaperQuestionController {
    @Resource
    private PaperQuestionService paperQuestionService;

    /**
     * 试卷列表
     */
    @RequestMapping("/admin/paperQuestion/ajax/list")
    @ResponseBody
    public Jr list(@RequestBody PaperQuestionDto paperQuestionDto) {
        paperQuestionService.listPage(paperQuestionDto);
        return Jr.yesd(paperQuestionDto);
    }

    /**
     * 选择试题页面
     */
    @RequestMapping("/admin/paperQuestion/select/question/list/{paperId}/{type}")
    public String selectQuestion(@PathVariable String paperId, @PathVariable String type, Model model) {
        model.addAttribute("paperId", paperId);
        model.addAttribute("type", type);
        return "admin/paperQuestion/admin-paperQuestion-select-question-list";
    }

    /**
     * 列表查询
     */
    @RequestMapping("/admin/paperQuestion/ajax/select/question/list")
    @ResponseBody
    public Jr qstListPage(@RequestBody QuestionDto query) {
        paperQuestionService.qstListPage(query);
        return Jr.yesd(query);
    }

    /**
     * 列表查询
     */
    @RequestMapping("/admin/paperQuestioin/ajax/save")
    @ResponseBody
    public Jr ajaxSave(PaperQuestionDto paperQuestionDto, HttpServletRequest request) {
        paperQuestionDto.setCurrentUserId(UserUtils.getUserId(request));
        paperQuestionService.save(paperQuestionDto);
        return Jr.yes();
    }

    /**
     * 列表查询
     */
    @RequestMapping("/admin/paperQuestioin/ajax/update")
    @ResponseBody
    public Jr ajaxUpdate(PaperQuestionDto paperQuestionDto, HttpServletRequest request) {
        paperQuestionDto.setCurrentUserId(UserUtils.getUserId(request));
        paperQuestionService.updateScore(paperQuestionDto);
        return Jr.yes();
    }

    /**
     * 列表查询
     */
    @RequestMapping("/admin/paperQuestioin/ajax/delete")
    @ResponseBody
    public Jr ajaxDelete(@RequestParam String[] ids,@RequestParam String paperId,HttpServletRequest request) {
        PaperQuestionDto paperQuestionDto = new PaperQuestionDto();
        paperQuestionDto.setCurrentUserId(UserUtils.getUserId(request));
        paperQuestionDto.setIds(ids);
        paperQuestionDto.setPaperId(paperId);
        paperQuestionService.deleteExtend(paperQuestionDto);
        return Jr.yes();
    }

}
